<?php

class ControllerToolExport extends Controller
{
    private $error = array();
    private $root_path = "";
    public function index()
    {
        $this->load->language('tool/export');
        $this->document->setTitle($this->language->get('heading_title'));
        $this->load->model('tool/export');
        $_root_path = explode("admin", DIR_APPLICATION);
        $this->root_path = $_root_path[0];  
        if (($this->request->server['REQUEST_METHOD'] == 'POST') && ($this->validate()))
        {
            if(!isset($this->request->post['is_upload']))
            {
                $file = $this->root_path . "import/" . $this->request->post['file_ajax'];
            } else{
                $file = $this->root_path . "import/" . $this->request->post['localfile'];
            }
            if ($this->model_tool_export->upload($file, $this->request->post['action'], (isset($this->request->post['byrow']) ? true : false)))
            {
                $this->session->data['success'] = $this->language->get('text_success');
                $this->redirect($this->url->link('tool/export', 'token=' . $this->session->data['token'], 'SSL'));
            } else
            {
                $this->error['warning'] = $this->language->get('error_upload');
            }
        }

        $this->data['heading_title'] = $this->language->get('heading_title');
        $this->data['entry_restore'] = $this->language->get('entry_restore');
        $this->data['entry_description'] = $this->language->get('entry_description');
        $this->data['button_import'] = $this->language->get('button_import');
        $this->data['button_export'] = $this->language->get('button_export');
        $this->data['tab_general'] = $this->language->get('tab_general');

        if (isset($this->error['warning']))
        {
            $this->data['error_warning'] = $this->error['warning'];
        } else
        {
            $this->data['error_warning'] = '';
        }

        if (isset($this->session->data['success']))
        {
            $this->data['success'] = $this->session->data['success'];

            unset($this->session->data['success']);
        } else
        {
            $this->data['success'] = '';
        }

        $this->data['breadcrumbs'] = array();

        $this->data['breadcrumbs'][] = array(
            'text' => $this->language->get('text_home'),
            'href' => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
            'separator' => false);

        $this->data['breadcrumbs'][] = array(
            'text' => $this->language->get('heading_title'),
            'href' => $this->url->link('tool/export', 'token=' . $this->session->data['token'], 'SSL'),
            'separator' => ' :: ');

        $this->data['action'] = $this->url->link('tool/export', 'token=' . $this->session->data['token'], 'SSL');

        $this->data['export'] = $this->url->link('tool/export/download', 'token=' . $this->session->data['token'], 'SSL');
        $this->data['local_excel'] = $this->scanDirExcel();
        $this->template = 'tool/export.tpl';
        $this->children = array(
            'common/header',
            'common/footer',
            );
        $this->response->setOutput($this->render());
    }

    public function scanDirExcel()
    {
        //path to directory to scan
        $directory = $this->root_path . "import/";

        //get all image files with a .jpg extension.
        $xlss = glob($directory . "*.xls");
        //print each file name
        $files = array();
        foreach ($xlss as $xls)
        {
            $f = explode($this->root_path . "import/", $xls);
            $check = explode(".", $f[1]);
            if(end($check) == "xls")
                $files[] = $f[1];
        }
        return $files;
    }
    public function getDirExcelAjax()
    {
        $_root_path = explode("admin", DIR_APPLICATION);
        $this->root_path = $_root_path[0];            
        $lists = $this->scanDirExcel();
        echo json_encode(array('error' => '1', 'data' => $lists));
        exit;
    }
    public function download()
    {
        if ($this->validate())
        {

            // set appropriate memory and timeout limits
            ini_set("memory_limit", "128M");
            set_time_limit(1800);

            // send the categories, products and options as a spreadsheet file
            $this->load->model('tool/export');
            $this->model_tool_export->download();

        } else
        {

            // return a permission error page
            return $this->forward('error/permission');
        }
    }


    private function validate()
    {
        if (!$this->user->hasPermission('modify', 'tool/export'))
        {
            $this->error['warning'] = $this->language->get('error_permission');
        }

        if (!$this->error)
        {
            return true;
        } else
        {
            return false;
        }
    }
}

?>